package com.qianfeng.smartmetting.controller;


import com.qianfeng.smartmetting.util.CommonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;

@RestController
public class LoginController {

    @PostMapping("/file/upload")
    public Map uploadFile(@RequestParam("file") MultipartFile file){
        try{
            String fileName=file.getOriginalFilename();
            System.out.println("文件名："+fileName);
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            System.out.println("后缀名："+suffixName);
            String basePath="D:/file/";

            Long systime=System.currentTimeMillis();
            String filePath = "D:/file/"+systime+suffixName;
            String path = basePath+systime+suffixName;
            File dest = new File(path);
            if(!dest.getParentFile().exists()){
                dest.getParentFile().mkdirs();
            }
            file.transferTo(dest);
            return CommonResult.ok("上传成功",filePath);
        }catch (Exception e){
            return CommonResult.error(-1,e.getMessage());
        }
    }

}
